<?php
require_once BaseUtil::getModulePath("base") . '/services/BaseService.php';
require_once BaseUtil::getModulePath("acl") . '/models/RolePermissionsDao.php';
require_once BaseUtil::getModulePath("acl") . '/models/RolesDao.php';
require_once BaseUtil::getModulePath("acl") . '/models/OperationsDao.php';
require_once BaseUtil::getModulePath("acl") . '/models/ResourcesDao.php';
require_once BaseUtil::getModulePath("acl") . '/models/UserDao.php';
require_once BaseUtil::getModulePath("acl") . '/models/UserPermissionsDao.php';
class AclService extends BaseService
{
    private $rolePermissionDao;
    private $rolesDao;
    private $resourcesDao;
    private $operationsDao;
    private $userDao;
    private $userPermissionsDao;
    public function __construct ()
    {
        parent::__construct();
        $this->rolePermissionDao = new RolePermissionsDao();
        $this->rolesDao = new RolesDao();
        $this->resourcesDao = new ResourcesDao();
        $this->operationsDao = new OperationsDao();
        $this->userDao = new UserDao();
        $this->userPermissionsDao = new UserPermissionsDao();
        $this->userPermissionsDao->setDb($this->db);
        $this->rolePermissionDao->setDb($this->db);
        $this->rolesDao->setDb($this->db);
        $this->resourcesDao->setDb($this->db);
        $this->operationsDao->setDb($this->db);
        $this->userDao->setDb($this->db);
    }
    public function getRoleById ($role_id)
    {
        return $this->rolesDao->getRoleById($role_id);
    }
    public function getPermissionById ($admin_id, $permission = null)
    {
        return $this->rolePermissionDao->getPermissionById($admin_id, $permission);
    }
    public function checkPermissionExist ($permission_id, $role_id)
    {
        return $this->rolePermissionDao->checkPermissionExist($permission_id, $role_id);
    }
    public function getPermissionsByRoleId ($role_id)
    {
        return $this->rolePermissionDao->getPermissionsByRoleId($role_id);
    }
    public function checkedPermission ($role_id, $controller, $aciton)
    {
        $permissions = $this->rolePermissionDao->getPermissionByRoleId($role_id);
        $controller_id = $this->resourcesDao->getResourceId($controller);
        $aciton_id = $this->operationsDao->getOperationId($aciton);
        foreach ($permissions as $row) {
            if ($row['resource_id'] == $controller_id && $row['operate_id'] == $aciton_id) {
                return true;
            }
        }
        return false;
    }
    public function updateRoleName ($role_id, $role_name)
    {
        return $this->rolesDao->updateRoleName($role_id, $role_name);
    }

    public function updateRole($role,$roleId,$permissions){
         $this->rolesDao->updateRole($role,$roleId);
         $this->rolePermissionDao->removeByRoleId($roleId);
         foreach($permissions as $permission){
            $this->rolePermissionDao->insertData(array('role_id' => $roleId,'permission_key' => $permission));
         }
    }
    
    public function addRole ($role,$permissions)
    {
        $roleId = $this->rolesDao->insertData($role);
        foreach($permissions as $permission){
            $this->rolePermissionDao->insertData(array('role_id' => $roleId,'permission_key' => $permission));
        }
    }
    public function addPermissionToRole ($params)
    {
        return $this->rolePermissionDao->insert($params);
    }
    public function deletePermission ($role_id, $permission_id)
    {
        return $this->rolePermissionDao->deletePermission($role_id, $permission_id);
    }
    public function checkPermissionDeleted ($permission_id, $role_id)
    {
        return $this->rolePermissionDao->checkPermissionDeleted($permission_id, $role_id);
    }
    public function activePermission ($permission_id, $role_id)
    {
        return $this->rolePermissionDao->activePermission($permission_id, $role_id);
    }
    public function deleteRole ($role_id)
    {
        $this->rolePermissionDao->deletePermission($role_id);
        $this->rolesDao->deleteRole();
        return;
    }
    public function deleteAllRoles($ids){
         foreach($ids as $id){
            if(!empty($id)){               
                $this->rolePermissionDao->deleleByRoleId($id);
                $this->rolesDao->deleteRole($id);
            }
        }
    }
    public function getUserById ($user_id)
    {
        return $this->userDao->getUserById($user_id);
    }
    public function getAllRoles ()
    {
        return $this->rolesDao->getAllRoles();
    }
    public function updateUser ($set, $user_id)
    {
        return $this->userDao->updateUser($set, $user_id);
    }
    public function deleteUser ($user_id)
    {
        return $this->userDao->deleteUser($user_id);
    }
    public function getPermissionsByUserId ($user_id)
    {
        return $this->userPermissionsDao->getPermissionsByUserId($user_id);
    }
    public function updatePermission ($user, $permissions, $user_id)
    {
        $this->userDao->updateUser($user['set'], $user['where']);
        $this->userPermissionsDao->deletePermission($user_id);
        foreach ($permissions as $row) {
            $insert['permission_id'] = $row;
            $insert['user_id'] = $user_id;
            $this->userPermissionsDao->insert($insert);
        }
    }
    public function addUser ($insert, $permissions)
    {
        $user_id = $this->userDao->insert($insert);
        foreach ($permissions as $row) {
            $inArr['permission_id'] = $row;
            $inArr['user_id'] = $user_id;
            $this->userPermissionsDao->insert($inArr);
        }
    }
    public function delete($id) {
    	if(!empty($id)) {
    		$this->rolePermissionDao->deleleByRoleId($id);
            $this->rolesDao->deleteRole($id);
    	}
    }   
}
